# -*- coding: utf-8 -*-
from urllib import request,parse
import random

# Hhander=request.HTTPHandler(debuglevel=1);
# #支持https请求
# #Hhander=request.HTTPSHandler(debuglevel=1);
# opener=request.build_opener(Hhander);
# re=request.Request("http://www.baidu.com/");
# reponse=opener.open(re);
# buff=reponse.read();
# html=buff.decode("utf-8");
# print(html);



# 构建了两个代理Handler，一个有代理IP，一个没有代理IP
# httpproxy_handler = request.ProxyHandler({"http" : "114.91.164.97","User-Agent": "Mozilla...."});
# nullproxy_handler = request.ProxyHandler({});
# proxySwitch = True; #定义一个代理开关

# # 通过build_opener()方法使用这些代理Handler对象，创建自定义opener对象
# # 根据代理开关是否打开，使用不同的代理模式
# if proxySwitch:
#     opener = request.build_opener(httpproxy_handler);
# else:
#     opener = request.build_opener(nullproxy_handler);
# re = request.Request("https://blog.csdn.net/justheretobe/article/details/52937937");
# # 1. 如果这么写，只有使用opener.open()方法发送请求才使用自定义的代理，而urlopen()则不使用自定义代理。
# headers = {"User-Agent": "Mozilla...."};
# response = opener.open(re);
# # 2. 如果这么写，就是将opener应用到全局，之后所有的，不管是opener.open()还是urlopen() 发送请求，都将使用自定义代理。
# #request.install_opener(opener)
# # response = urlopen(re)
# buff=response.read();
# html=buff.decode("utf-8");
# print(html);

proxy_list = [
    {"http" : "114.91.164.97"},
    {"http" : "116.192.175.93"},
    {"http" : "116.192.175.93"},
    {"http" : "61.170.179.89"},
    {"http" : "210.22.176.146"},
    {"http" : "116.192.175.93"}
]

# 随机选择一个代理
proxy = random.choice(proxy_list)
# 使用选择的代理构建代理处理器对象
httpproxy_handler = request.ProxyHandler(proxy)
opener = request.build_opener(httpproxy_handler)
re = request.Request("https://blog.csdn.net/guozili1?t=1")
response = opener.open(re)
buff=response.read();
html=buff.decode("utf-8");
print(html);